package com.lrkj.back.controller;

import com.lrkj.back.entity.SendIm;
import com.lrkj.back.service.SendImservice;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daiwe on 2017/11/23.
 */
@Controller
@RequestMapping("/sendIm")
public class SendImController {

    @Autowired
    public SendImservice sendImservice;


    @RequestMapping("/page.do")
    public ModelAndView sendIm(){

        return new ModelAndView("/peizhi/songbao/song_list");

    }

    @RequestMapping("/editPage")
    public ModelAndView editPage(String sendId ,HttpServletRequest request,
                                 HttpServletResponse response){

        if(StringUtils.isNotBlank(sendId)) {
            Integer integer = Integer.valueOf(sendId);
         SendIm sendIm = sendImservice.selectByPrimaryKey(integer);
         request.setAttribute("sendIm",sendIm);
        }
        return new ModelAndView("/peizhi/songbao/song_edit");

    }

    @RequestMapping("/data")
    @ResponseBody
    public Map<String,Object> sendImData(){
        Map<String,Object> rtnMap = new HashMap<>();

        List<SendIm> sendImList = sendImservice.findAll(rtnMap);
        rtnMap.put("sendImList",sendImList);
        return rtnMap;

    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> sendImData(SendIm sendIm){
        Map<String,Object> rtnMap = new HashMap<>();
        Integer sendId = sendIm.getSendId();
        try{
            if(sendId!=null){
                sendImservice.updateByPrimaryKey(sendIm);
            }else{
                sendImservice.insert(sendIm);
            }
            rtnMap.put("status",true);
            rtnMap.put("msg","保存成功");
        }catch (Exception e){
            rtnMap.put("status",false);
            e.printStackTrace();
            rtnMap.put("msg","保存失败");
        }
        return rtnMap;

    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(String  sendId){
        Map<String,Object> rtnMap = new HashMap<>();
        try{
            if(sendId!=null){
                String [] sendIdArr = sendId.split(",");
                for (int i = 0; i < sendIdArr.length; i++) {
                    Integer integer = Integer.valueOf(sendIdArr[i]);
                    sendImservice.deleteByPrimaryKey(integer);
                }

            }
            rtnMap.put("status",true);
            rtnMap.put("msg","删除成功");
        }catch (Exception e){
            rtnMap.put("status",false);
            e.printStackTrace();
            rtnMap.put("msg","删除失败");
        }
        return rtnMap;

    }


}
